import string
import random
from JianShuBlog import settings
from django.core.mail import send_mail
from django.shortcuts import render


from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
import pymysql
from django.contrib.auth.hashers import make_password, check_password
from django.shortcuts import render, redirect


# 注册界面
def sign_up(request):
    return render(request, 'home/sign_up.html')

# 登录界面
def sign_in(request):
    return render(request, 'home/sign_in.html')

def home(request):
    return render(request, '/home/indexindex.html')

def save(request):
    # 标识当前用户是否已经存在{0：不存在，1：存在}
    regitered = 0

    a = request.GET

    # 获取信息
    username = a.get('username')
    password = a.get('password')
    password = make_password(password)
    phone = a.get('phone')
    email = a.get('email')

    # 连接数据库，进行数据验证
    db = pymysql.connect('127.0.0.1', 'root', '123456', 'jianshu')
    # 创建游标
    cursor = db.cursor()
    # SQL 语句
    sqlone = 'select * from jianshuapp_user'
    # 执行sql语句
    cursor.execute(sqlone)
    # 进行数据存储
    all_users = cursor.fetchall()
    # 判断注册的用户
    i = 0
    while i < len(all_users):
        if username in all_users[i]:
            regitered = 1
        i += 1

    if regitered == 0:
        sqltwo = 'insert into jianshuapp_user(username,phone,email,password) values (%s,%s,%s,%s)'
        cursor.execute(sqltwo, (username, phone, email, password))
        db.commit()
        cursor.close()
        db.close()
        print("注册成功")
        return HttpResponseRedirect('/sign_in/')
    else:
        cursor.close()
        db.close()
        return HttpResponse('该账户已经存在，请直接登录，忘记密码请联系管理员')


# 进行登录验证
def query(request):
    # 获得get请求
    a = request.GET

    # 获取信息
    username = a.get('username')
    password = a.get('password')
    db = pymysql.connect('127.0.0.1', 'root', '123456', 'jianshu')
    cursor = db.cursor()
    sqlone = 'select username , phone , email, password  from jianshuapp_user'
    cursor.execute(sqlone)
    all_users = cursor.fetchall()
    cursor.close()
    db.close()
    had_user = 0
    i = 0
    while i < len(all_users):
        if username == all_users[i][0] or username == all_users[i][1] or username == all_users[i][2]:
            if check_password(password, all_users[i][3]):
                had_user = 1
        i += 1
    if had_user == 1:
        return HttpResponse('登录成功')
        # 重定向到JianShuBlog首页
        # return HttpResponseRedirect('/index/')
    else:
        return HttpResponse('用户名或密码有误，请重新输入')

def check_email(request):
     checktext = ''
     checktexts =string.ascii_letters + string.digits
     for i in range(6):
         checktext += random.choice(checktexts)

     message = '您好！欢迎使用邮箱验证！您的邮箱验证码为：' + str(checktext)
     yourMail = [request.GET.get('email')]
     send_mail(
         '简书邮箱注册验证码',
         message,
         settings.EMAIL_HOST_USER,
         list(yourMail),
         fail_silently = False,
     )
     request.session['checkemail'] = checktext
     return JsonResponse({'check':checktext})

def check_username(request):
    username = request.GET.get('username')
    db = pymysql.connect('127.0.0.1', 'root', '123456', 'jianshu')
    cursor = db.cursor()
    sqlthree = 'select username from jianshuapp_user'
    cursor.execute(sqlthree)
    all_users = cursor.fetchall()
    cursor.close()
    db.close()
    had_user = 0
    i = 0
    while i < len(all_users):
        if username == all_users[i][0] :
            had_user = 1
        i += 1
    if had_user == 1:
        return username









